package com.ruicar.afs.cloud.apply.archive.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.apply.archive.condition.ImageCondition;
import com.ruicar.afs.cloud.apply.archive.service.DelayMngService;
import com.ruicar.afs.cloud.apply.archive.service.WaybillService;
import com.ruicar.afs.cloud.archive.dto.DelayDto;
import com.ruicar.afs.cloud.archive.dto.DocumentDto;
import com.ruicar.afs.cloud.archive.feign.ArchiveApiFeign;
import com.ruicar.afs.cloud.archive.result.ArchiveCommon.ArchivedPicture;
import com.ruicar.afs.cloud.archive.result.ArchiveDelayListDto.ArchivedDelayDetail;
import com.ruicar.afs.cloud.archive.result.ArchiveDelayListDto.ArchivedDelayEdit;
import com.ruicar.afs.cloud.archive.result.ArchivedPageDto;
import com.ruicar.afs.cloud.archive.result.ArchivedResultDto;
import com.ruicar.afs.cloud.archive.service.ArchiveService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 长城延期管理接口实现类
 *
 * @author 耿继辉
 */
@Service
@AllArgsConstructor
public class DelayMngServiceImpl implements DelayMngService {

    private final ArchiveApiFeign archiveApiFeign;
    private ArchiveService archiveService;
    private WaybillService waybillService;

    /**
     * 获取token信息
     *
     * @return
     */
    @Override
    public String getTokenInfo() {
        String authorization = archiveService.getAuthorization();
        return authorization;
    }


    /**
     * 获取延期申请列表
     *
     * @param dto
     * @return
     */
    @Override
    public IResponse getDelayList(DelayDto dto) {
        Map<String, String> headers = new HashMap<>(2);
        headers.put("Authorization", this.getTokenInfo());
        try {
            ArchivedResultDto json =  archiveApiFeign.getDelayList(dto, headers);
            if(1==json.getSuccess()){
                ArchivedPageDto archivedPageDto = JSON.parseObject(JSON.toJSONString(json.getData()), ArchivedPageDto.class);
                List<ArchivedDelayDetail> list = JSON.parseArray(JSON.toJSONString(archivedPageDto.getData()), ArchivedDelayDetail.class);
                //分页处理
                IPage iPage = new Page();
                iPage.setRecords(list);
                iPage.setPages(archivedPageDto.getPageIndex());
                iPage.setSize(archivedPageDto.getPageSize());
                iPage.setTotal(archivedPageDto.getRecordsTotal());
                return IResponse.success(iPage);
            }else{
                return IResponse.fail(json.getMessage());
            }
        }catch (Exception e){
            return IResponse.fail("接口异常，请联系管理员！");
        }
    }

    /**
     * 延期申请-提交申请和暂存
     *
     * @param dto
     * @return
     */
    @Override
    public IResponse submitDelay(DelayDto dto) {
        Map<String, String> headers = new HashMap<>(2);
        headers.put("Authorization", this.getTokenInfo());
        try {
            ArchivedResultDto json =  archiveApiFeign.submitDelay(dto, headers);
            if(1==json.getSuccess()){
                return IResponse.success("提交成功");
            }else{
                return IResponse.fail(json.getMessage());
            }
        }catch (Exception e){
            return IResponse.fail("接口异常，请联系管理员！");
        }
    }

    /**
     * 申请延期申请
     *
     * @param dto
     * @return
     */
    @Override
    public IResponse newDelay(DelayDto dto) {
        Map<String, String> headers = new HashMap<>(2);
        headers.put("Authorization", this.getTokenInfo());
        try {
            ArchivedResultDto archivedResultDto =  archiveApiFeign.newDelay(dto, headers);
            if(1==archivedResultDto.getSuccess()){
                ArchivedDelayEdit archivedDelayEdit = JSON.parseObject(JSON.toJSONString(archivedResultDto.getData()), ArchivedDelayEdit.class);
                return IResponse.success(archivedDelayEdit);
            }else{
                return IResponse.fail(archivedResultDto.getMessage());
            }
        }catch (Exception e){
            return IResponse.fail(e.toString());
        }
    }

    /**
     * 编辑(查看) 延期申请id
     *
     * @param dto
     * @return
     */
    @Override
    public IResponse lookDelay(DelayDto dto) {
        Map<String, String> headers = new HashMap<>(2);
        headers.put("Authorization", this.getTokenInfo());
        try {
            ArchivedResultDto archivedResultDto =  archiveApiFeign.lookDelay(dto, headers);
            if(1==archivedResultDto.getSuccess()){
                ArchivedDelayEdit archivedDelayEdit = JSON.parseObject(JSON.toJSONString(archivedResultDto.getData()), ArchivedDelayEdit.class);
                List<ArchivedPicture> impictureMd5List = new ArrayList();
                List<ArchivedPicture> archivedDocEditPictureList = archivedDelayEdit.getMd5s();
                BASE64Encoder encoder = new BASE64Encoder();
                for(ArchivedPicture archivedPicture : archivedDocEditPictureList){
                    ImageCondition imageCondition = new ImageCondition();
                    imageCondition.setMd5(archivedPicture.getMd5());
                    byte[] md5 = waybillService.downFile(imageCondition);
                    archivedPicture.setMd5Byte("data:image/jpeg;base64,"+encoder.encode(md5));
                    impictureMd5List.add(archivedPicture);
                }
                archivedDelayEdit.setMd5List(impictureMd5List);
                return IResponse.success(archivedDelayEdit);
            }else{
                return IResponse.fail(archivedResultDto.getMessage());
            }
        }catch (Exception e){
            return IResponse.fail(e.toString());
        }
    }

    /**
     * 删除草稿状态的延期申请
     *
     * @param dto
     * @return
     */
    @Override
    public IResponse deleteDelay(DelayDto dto) {
        Map<String, String> headers = new HashMap<>(2);
        headers.put("Authorization", this.getTokenInfo());
        try {
            ArchivedResultDto archivedResultDto =  archiveApiFeign.deleteDelay(dto, headers);
            if(1==archivedResultDto.getSuccess()){
                return IResponse.success("删除成功");
            }else{
                return IResponse.fail(archivedResultDto.getMessage());
            }
        }catch (Exception e){
            return IResponse.fail(e.toString());
        }
    }

    /**
     * 银行卡/身份证打印接口
     *
     * @param dto
     * @return
     */
    @Override
    public IResponse printDocPicture(DocumentDto dto) {
        Map<String, String> headers = new HashMap<>(2);
        headers.put("Authorization", this.getTokenInfo());
        try {
            ArchivedResultDto archivedResultDto =  archiveApiFeign.printDocPicture(dto, headers);
            if("1".equals(archivedResultDto.getSuccess())){
                return IResponse.success(true);
            }else{
                return IResponse.fail(archivedResultDto.getMessage());
            }
        }catch (Exception e){
            return IResponse.fail(e.toString());
        }
    }

}
